package com.Eric.test;

import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.junit.Test;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.concurrent.ForkJoinPool;

/**
 * @author Eric
 * @Create 2020-09-18  15:47
 * @Version project-workspace-0912
 **/
public class POI {


    /**
     * 读取数据
     * @throws IOException
     */
    @Test
    public void test01() throws IOException {
        //获取工作簿
        XSSFWorkbook sheets = new XSSFWorkbook("D:/BaiduNetdiskDownload/All-WorkSpace/project-workspace-0912/meinian_parent/meinian_common/src/test/java/com/Eric/file/test01.xlsx");
        //获取工作表
        /*XSSFSheet sheet1 = sheets.getSheetAt(0);*/
        XSSFSheet sheet1 = sheets.getSheet("sheet1");

        //遍历工作表获得行对象
        for (Row cells : sheet1) {
            //遍历获得单元格对象
            for (Cell cell : cells) {
                String value = cell.getStringCellValue();
                System.out.print(value+"\t");
            }
            System.out.println();
        }

    }

    /**
     * 获取最后一行，行数  读取
     * @throws IOException
     */
    @Test
    public  void test02() throws IOException {
        //获取工作簿
        XSSFWorkbook table = new XSSFWorkbook(
                "D:/BaiduNetdiskDownload/All-WorkSpace/project-workspace-0912/meinian_parent/meinian_common/src/test/java/com/Eric/file/test01.xlsx");
        XSSFSheet sheet1 = table.getSheetAt(0);
        int lastRowNum = sheet1.getLastRowNum();
        for (int i = 0; i <lastRowNum ; i++) {
            XSSFRow row = sheet1.getRow(i);
            short lastCellNum = row.getLastCellNum();
            for (int j = 0; j <lastCellNum ; j++) {
                String stringCellValue = row.getCell(j).getStringCellValue();
                System.out.print(stringCellValue+"\t");
            }
            System.out.println();
        }
    }

    @Test
    public void test03() throws Exception {
//        1.创建工作簿对象
//        2.创建工作表对象
//        3.创建行对象
//        4.创建列(单元格)对象, 设置内容
//        5.通过输出流将workbook对象下载到磁盘
        XSSFWorkbook workbook = new XSSFWorkbook();
        XSSFSheet sheet = workbook.createSheet("财务报表");
        XSSFRow row = sheet.createRow(0);
        row.createCell(0).setCellValue("编号");
        row.createCell(1).setCellValue("姓名");
        row.createCell(2).setCellValue("性别");
        for (int i = 1; i < 3 ; i++) {
            XSSFRow row1 = sheet.createRow(i);
                row1.createCell(0).setCellValue(i);
                row1.createCell(1).setCellValue("妙啊");
                row1.createCell(2).setCellValue("？？");
        }
        //通过输出流将workbook对象下载到磁盘
        FileOutputStream out = new FileOutputStream("D:/BaiduNetdiskDownload/All-WorkSpace/project-workspace-0912/meinian_parent/meinian_common/src/test/java/com/Eric/file/test02.xlsx");
        workbook.write(out);
        out.flush();
        out.close();
        workbook.close();
    }
}
